package httpsrv

import (
	"net/http"
	"cloud_worm/worm/config"
	"time"
	"github.com/golang/glog"
)

/**
 在worm的启动模式中，存在两种模式，其中一种即为：httpsrv模式。
 该模式下：
	1. 负责对于client的操作动作：stop、start、restart、update等操作
 */

func HttpServer() error {
	mux := http.NewServeMux()

	mux.HandleFunc("/worm/client/stop", func(rw http.ResponseWriter, req *http.Request) {
		stopClient(rw, req)
	})
	mux.HandleFunc("/worm/client/start", func(rw http.ResponseWriter, req *http.Request) {
		startClient(rw, req)
	})
	mux.HandleFunc("/worm/client/restart", func(rw http.ResponseWriter, req *http.Request) {
		restartClient(rw, req)
	})
	mux.HandleFunc("/worm/client/update", func(rw http.ResponseWriter, req *http.Request) {
		updateClient(rw, req)
	})
	mux.HandleFunc("/worm/client/install", func(rw http.ResponseWriter, req *http.Request) {
		installClient(rw, req)
	})

	httpSrv := &http.Server{
		Addr: config.CfgInst.LocalAddr + ":" + config.CfgInst.StartConfig.(*config.HttpSrvConfig).HttpServer.WebPort,
		Handler: mux,
		IdleTimeout: 90 * time.Second,
	}
	glog.Infof("http server: %s", config.CfgInst.LocalAddr + ":" + config.CfgInst.StartConfig.(*config.HttpSrvConfig).HttpServer.WebPort)
	if err := httpSrv.ListenAndServe(); err != nil {
		return err
	}
	return nil
}

// 停止客户端
func stopClient(rw http.ResponseWriter, req *http.Request) {
	req.ParseForm()

	// TODO: 鉴权处理

	return
}

// 启动客户端
func startClient(rw http.ResponseWriter, req *http.Request) {
	req.ParseForm()

	// TODO: 鉴权处理

	return
}

// 重启client
func restartClient(rw http.ResponseWriter, req *http.Request) {
	req.ParseForm()

	// TODO: 鉴权处理

	return
}

// update client
func updateClient(rw http.ResponseWriter, req *http.Request) {
	req.ParseForm()

	// TODO: 鉴权处理

	return
}

func installClient(rw http.ResponseWriter, req *http.Request) {
	req.ParseForm()

	// TODO: 鉴权处理

	return
}